package com.micro.frog.fortune.executor.cwl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.micro.frog.fortune.dao.entity.CwlAnalyPo;
import com.micro.frog.fortune.dao.entity.CwlRecordPo;
import com.micro.frog.fortune.dao.mapper.CwlAnalyMapper;
import com.micro.frog.fortune.dao.mapper.CwlRecordMapper;
import com.micro.frog.fortune.model.base.Resp;
import com.micro.frog.fortune.model.cwl.CwlReq;
import com.micro.frog.fortune.sdk.cwl.enums.CwlAnalyType;
import com.micro.frog.fortune.sdk.cwl.enums.CwlColor;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 号码分析
 *
 * @author jason.lv
 */
@Slf4j
@Service
public class CwlAnalyExecutor {

    @Autowired
    private CwlRecordMapper recordMapper;
    @Autowired
    private CwlAnalyMapper analyMapper;

    public Resp execute(CwlReq request) {

        QueryWrapper<CwlRecordPo> wrapper = new QueryWrapper<>();
        wrapper.eq("type", request.getName());
        List<CwlRecordPo> pos = recordMapper.selectList(wrapper);

        if (CollUtil.isNotEmpty(pos)){
            for (CwlRecordPo po : pos) {
                List<String> reds = po.getReds();
                List<String> blues = po.getBlues();
                for (int i = 0; i < reds.size(); i++) {
                    CwlAnalyPo analyPo = new CwlAnalyPo();
                    analyPo.setName(po.getType());
                    analyPo.setType(CwlAnalyType.sns);
                    analyPo.setNum(reds.get(i));
                    analyPo.setIndex(i);
                    analyPo.setColor(CwlColor.red);
                    analyMapper.insert(analyPo);
                }

                for (int i = 0; i < blues.size(); i++) {
                    CwlAnalyPo analyPo = new CwlAnalyPo();
                    analyPo.setName(po.getType());
                    analyPo.setType(CwlAnalyType.sns);
                    analyPo.setNum(blues.get(i));
                    analyPo.setIndex(i);
                    analyPo.setColor(CwlColor.blue);
                    analyMapper.insert(analyPo);
                }
            }
        }
        return Resp.generate();
    }
}

